import java.util.Scanner;

public class ATM03 {
    public static Scanner sc = new Scanner(System.in);
    public static String[][] users = {
            {"abc","123","1000"},
            {"123","abc","2000"},
            {"lht","hmp01","3000"},
            {"zjh","hmp02","4000"},
            {"zyd","hmp03","5000"},
    };
    public static int index;
    public static void process(){
        System.out.println("欢迎进入xxx银行");
        login();
        menu();
    }
    /**
     * 登录
     */
    public static void login(){
        for (int i = 0;i < 3;i++){
            System.out.println("请输入账号：");
            String account = sc.next();
            System.out.println("请输入密码：");
            String password = sc.next();
            for (int j = 0;j < users.length;j++){
                if (account.equals(users[j][0]) && password.equals(users[j][1])){
                    index = j;
                    System.out.println("登录成功");
                    break;
                }
            }
            System.out.println("账号或密码错误，请重新输入（还剩"+(2-i)+"次机会）");
            if (i == 2){
                System.out.println("连续三次输入有误，系统退出");
                System.exit(0);
            }
        }
    }

    /**
     * 菜单
     */
    public static void menu(){
        while (true){
            System.out.println("请选择：1-存款 2-取款 3-查询 4-转账 5-修改密码 6-切换用户 7-退出");
            int input = sc.nextInt();
            if (input == 1){
                save();
            }else if (input == 2){
                get();
            }else if (input == 3){
                search();
            }else if (input == 4){
                transfer();
            }else if (input == 5){
                changePwd();
            }else if (input == 6){
                login();
            }else if (input == 7){
                System.out.println("谢谢您的使用，再见");
                System.exit(0);
            }else {
                System.out.println("输入有误");
            }
        }
    }

    /**
     * 存钱
     */
    public static void save(){
        System.out.println("请输入您要存款的金额：");
        double blance = sc.nextDouble();
        if (blance > 0){
            users[index][2] = Double.parseDouble(users[index][2]) + blance + "";
            System.out.println("存款成功");
        }else {
            System.out.println("请输入正确存款的金额");
        }
    }

    /**
     * 取钱
     */
    public static void get(){
        System.out.println("请输入您要取款的金额：");
        double blance = sc.nextDouble();
        if (blance > 0){
            users[index][2] = Double.parseDouble(users[index][2]) - blance + "";
            System.out.println("取款成功");
        }else {
            System.out.println("余额不足");
        }
    }

    /**
     * 查询
     */
    public static void search(){
        System.out.println("您的余额为：" + users[index][2]);
    }

    /**
     * 转账
     */
    public static void transfer(){
        System.out.println("请输入对方账号：");
        String otherAccount = sc.next();
        // 不能转账给自己
        if(otherAccount.equals(users[index][0])){
            System.out.println("不能转给自己");
            menu();
        }
        // 判断对方账号是否存在
        boolean isExist = false;
        int otherIndex = 0;
        for(int i = 0;i < users.length;i++){
            if(users[i][0].equals(otherAccount)){
                isExist = true;
                otherIndex = i;
                break;
            }
        }
        if(!isExist){
            System.out.println("输入的对方账号错误");
            menu();
        }
        System.out.println("请输入转账金额：");
        double blance = sc.nextDouble();
        // 判断余额是否足够
        if(blance > Double.parseDouble(users[index][2])){
            System.out.println("余额不足");
            menu();
        }
        // 从当前账号上扣去转账的金额
        users[index][2] = Double.parseDouble(users[index][2]) - blance + "";
        // 将转账的金额加在对方账号上
        users[otherIndex][2] = Double.parseDouble(users[otherIndex][2]) + blance + "";

        System.out.println("转账成功");
    }

    /**
     * 改密
     */
    public static void changePwd(){
        System.out.println("请输入旧密码：");
        String pwd = sc.next();
        if (pwd.equals(users[index][1])){
            System.out.println("请输入新密码：");
            String pwd2 = sc.next();
            System.out.println("请再次输入：");
            String pwd3 = sc.next();
            if (pwd2.equals(pwd3)){
                users[index][1] = pwd2;
                System.out.println("修改成功");
                login();
            }else {
                System.out.println("两次输入的密码不同，请重新输入");
            }
        }else {
            System.out.println("旧密码输入错误");
        }
    }

    public static void main(String[] args) {
        process();
    }

}
